using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using System.IO;

namespace SharepointListSecuritySP2013.Office.Features.OfficeListSecuritySP2013
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("60efa828-ea9c-4247-aa9f-83f9301f8981")]
    public class OfficeListSecuritySP2013EventReceiver : SPFeatureReceiver
    {
        private static string url = SPUtility.GetGenericSetupPath("ISAPI");
        private static string listServicePath = url + "//lists.asmx";
        private static string listServicePathBackupPath = url + "//lists.asmx.backup";
        private static string dafaultListService = "<%@ WebService Language=\"c#\" Class=\"Microsoft.SharePoint.SoapServer.Lists,stssoap\" %>";
        private static string listService = "<%@ WebService Language=\"c#\" Class=\"SharePointListSecurity.Office.Lists, SharePointListSecurity.Office, Version=1.0.0.0, Culture=neutral, PublicKeyToken=4c2072c14559aa02\" %>";

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    File.Move(listServicePath, listServicePathBackupPath);
                    var stream = new StreamWriter(listServicePath);
                    stream.WriteLine(listService);
                    stream.Close();
                });
            }
            catch (Exception)
            {
            }

        }


        // Uncomment the method below to handle the event raised before a feature is deactivated.

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
              {
                  File.Delete(listServicePath);
                  File.Move(listServicePathBackupPath, listServicePath);
              });
            }
            catch (Exception)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
             {
                 var stream = new StreamWriter(listServicePath);
                 stream.WriteLine(dafaultListService);
                 stream.Close();
             });
            }
        }





        // Uncomment the method below to handle the event raised after a feature has been installed.

        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}


        // Uncomment the method below to handle the event raised before a feature is uninstalled.

        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}

        // Uncomment the method below to handle the event raised when a feature is upgrading.

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
